/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-11-10 16:51:31
 * @LastEditTime: 2023-01-09 10:33:14
 */
import {
  SET_SELECTED_ITEM,
  SET_TEMPLATE_LIST,
  SET_TPL_TYPE,
  SET_METHOD_TYPE,
  ADD_TEMPLATE,
  EDIT_TEMPLATE,
  DELETE_TEMPLATE,
  SET_SELECT_AT,
  SET_SELECT_FOND
} from '../constants';
import { delItemByIdArray, insertItem, updateItemById } from '@/utils/list';

export const TemplateManageReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 表格加载loading
     */
    case SET_SELECT_FOND:
      return {
        ...state,
        selectFond: action.data
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };
    /* 模板类型选择 */
    case SET_TPL_TYPE:
      return {
        ...state,
        tplType: action.data
      };
    /* 导入导出类型 */
    case SET_METHOD_TYPE:
      return {
        ...state,
        methodType: action.data
      };
    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };
    /**
     * @description: 加载元数据列表
     * @param {Array} data 表格数据
     */
    case SET_TEMPLATE_LIST: {
      return {
        ...state,
        templateList: action.data
      };
    }
    /* 新增列表 */
    case ADD_TEMPLATE:
      return { ...state, templateList: insertItem(action.data, state.templateList) };
    /* 修改列表 */
    case EDIT_TEMPLATE:
      return {
        ...state,
        templateList: updateItemById(action.data.id, action.data, state.templateList)
      };

    /**
     * @description: 删除元数据条目
     */
    case DELETE_TEMPLATE: {
      return {
        ...state,
        templateList: delItemByIdArray(action.ids, state.templateList)
      };
    }
    default:
      console.log('untouched', action);
      return state;
  }
};
